package persistence;

import persistence.util.ProductionConnectionProvider;

public class DAOFactoryJdbcImpl extends DAOFactoryJdbcImplBase implements DAOFactory {
	private SubscriptionDAO subscriptionDAO = null;
	private BindingDetailsDAO bindingDetailsDAO = null;
	private CostUnitDAO costUnitDAO = null;
	private PeriodicalDAO periodicalDAO = null;
	private PurchaserDAO purchaserDAO = null;
	private SupplierDAO supplierDAO = null;
	private PredefinedEntryDAO predefinedEntryDAO = null;
	private PredefinedParamDAO predefinedParamDAO = null;
	private LibraryLocationDAO libraryLocationDAO = null;
	private CityDAO cityDAO = null;
	private UserDAO userDAO = null;
	private RoleDAO roleDAO = null;

	public DAOFactoryJdbcImpl() {
		this("dao.properties");
	}
	
	public DAOFactoryJdbcImpl(String connectionPropertiesFile) {
		super(connectionPropertiesFile, new ProductionConnectionProvider());
	}
	
	/**
	 * 
	 */
	@Override
	public SubscriptionDAO getSubscriptionDAO() {
		if (this.subscriptionDAO == null) {
			SubscriptionDAOJdbcImpl subscriptionDAO = new SubscriptionDAOJdbcImpl();
			subscriptionDAO.setBindingDetailsDAO(new BindingDetailsDAOJdbcImpl());
			subscriptionDAO.setCostUnitDAO(getCostUnitDAO());
			subscriptionDAO.setPeriodicalDAO(getPeriodicalDAO());
			subscriptionDAO.setPurchaserDAO(getPurchaserDAO());		
			subscriptionDAO.setSupplierDAO(getSupplierDAO());
			subscriptionDAO.setPredefinedEntryDAO(getPredefinedEntryDAO());
			subscriptionDAO.setLibraryLocationDAO(getLibraryLocationDAO());
			this.subscriptionDAO = subscriptionDAO;
		}
		return subscriptionDAO;
	}
	
	/**
	 *
	 */
	public SupplierDAO getSupplierDAO() {
		if (this.supplierDAO == null) {
			SupplierDAOJdbcImpl supplierDAO = new SupplierDAOJdbcImpl();
			supplierDAO.setCityDAO(getCityDAO());
			this.supplierDAO = supplierDAO;
		}
		return this.supplierDAO;
	}
	
	public PredefinedEntryDAO getPredefinedEntryDAO() {
		if  (this.predefinedEntryDAO == null) {
			PredefinedEntryDAOJdbcImpl predefEntryDAO = new PredefinedEntryDAOJdbcImpl();
			predefEntryDAO.setPredefinedParamDAO(new PredefinedParamDAOJdbcImpl());
			this.predefinedEntryDAO = predefEntryDAO;
		}
		return this.predefinedEntryDAO;
	}

	public PredefinedParamDAO getPredefinedParamDAO() {
		if (this.predefinedParamDAO == null) {
			this.predefinedParamDAO = new PredefinedParamDAOJdbcImpl();			
		}
		return this.predefinedParamDAO;
	}
	
	public PeriodicalDAO getPeriodicalDAO() {
		if (this.periodicalDAO == null) {
			PeriodicalDAOJdbcImpl periodicalDAO = new PeriodicalDAOJdbcImpl();
			periodicalDAO.setPredefinedEntryDAO(getPredefinedEntryDAO());
			this.periodicalDAO = periodicalDAO;
		}
		return this.periodicalDAO;		
	}
	
	public CostUnitDAO getCostUnitDAO() {
		if (this.costUnitDAO == null) {
			CostUnitDAOJdbcImpl costUnitDAO = new CostUnitDAOJdbcImpl();
			costUnitDAO.setLibraryLocationDAO(getLibraryLocationDAO());
			this.costUnitDAO = costUnitDAO;
		}
		return costUnitDAO;
	}
	
	public LibraryLocationDAO getLibraryLocationDAO() {
		if (this.libraryLocationDAO == null) {
			LibraryLocationDAOJdbcImpl libraryLocationDAO = new LibraryLocationDAOJdbcImpl();
			libraryLocationDAO.setCityDAO(getCityDAO());
			this.libraryLocationDAO = libraryLocationDAO;
		}
		return this.libraryLocationDAO;
	}
	
	public CityDAO getCityDAO() {
		if (this.cityDAO == null) {
			this.cityDAO = new CityDAOJdbcImpl();
		}
		return this.cityDAO;
	}
	
	public PurchaserDAO getPurchaserDAO() {
		if (this.purchaserDAO == null) {
			PurchaserDAOJdbcImpl purchaserDAO = new PurchaserDAOJdbcImpl();
			purchaserDAO.setLibraryLocationDAO(getLibraryLocationDAO());
			this.purchaserDAO = purchaserDAO;
		}
		return this.purchaserDAO;
	}

	public UserDAO getUserDAO() {
		if (userDAO == null) {
			UserDAOJdbcImpl userDAO = new UserDAOJdbcImpl();
			userDAO.setLibraryLocationDAO(getLibraryLocationDAO());
			userDAO.setRoleDAO(getRoleDAO());
			this.userDAO = userDAO;
		}
		return this.userDAO;
	}

	public RoleDAO getRoleDAO() {
		if (roleDAO == null){
			RoleDAOJdbcImpl roleDAO = new RoleDAOJdbcImpl();
			this.roleDAO = roleDAO;
		}
		return this.roleDAO;
	}
	
}
